<!doctype html>
<html>
  <script type="application/javascript" src="laz-perf.js"></script>
  <script>
    var laszip;
      Module.onRuntimeInitialized = () => {
        laszip = new Module.LASZip();
        const buf = loadLaz('/point10.las.laz');
    };

    async function loadLaz(filename)
    {
        try
        {
            const res = await fetch(filename);
            const ab = await res.arrayBuffer();
            const buf = new Uint8Array(ab);
            handleLazBuf(buf);
        }
        catch (err)
        {
            console.log(err.message);
        }
    }

    function parsePoint(pointbuf, bufsize)
    {
        const p = new DataView(Module.HEAPU8.buffer, pointbuf, bufsize);
        console.log('X = ', p.getInt32(0, true));
        console.log('Y = ', p.getInt32(4, true));
        console.log('Z = ', p.getInt32(12, true));
    }

    function handleLazBuf(tempbuf)
    {
        const len = tempbuf.byteLength;
        const buf = Module._malloc(len);
        Module.HEAPU8.set(tempbuf, buf);

        laszip.open(buf, len);
        const pointCount = laszip.getCount();
        console.log('POINT COUNT = ', pointCount);

        const pointSize = laszip.getPointLength();
        var pointbuf = Module._malloc(pointSize);
        for (var i = 0; i < pointCount; ++i)
        {
            laszip.getPoint(pointbuf);
            parsePoint(pointbuf, pointSize);
        }
    }

  </script>
</html>
